package cn.engage.RocketMQ.Util;


import cn.engage.basic.config.BussinessException;
import cn.engage.order.domain.OrderPetAcquisition;
import cn.engage.order.mapper.OrderPetAcquisitionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: wwb
 * @Date: 2022年11月7日10:57:09
 * @Description: 消费者类
 * RocketMQMessageListener : 监听器，不带事物，用来接收消息的
 *      topic ：定位到哪一个topic
 *      consumerGroup ：指定哪一个组去消费
 *
 * RocketMQTransactionListener :监听器，带事物，用来接收消息的
 */
@Component
@Slf4j
@RocketMQMessageListener(topic = "spring_rocketMQ",consumerGroup = "group1")
public class MsgConsumer implements RocketMQListener<String> {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Override
    public void onMessage(String message) {
        log.info("msg:{}",message);

        // 根据订单号去查询,判断状态，有没有被修改
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findByOrderSn(message);
        // 判断订单号状态
        if (orderPetAcquisition.getState() == 0) {
            // 说明就在规定的时间里面没有进行上门，修改状态为-1
            orderPetAcquisition.setState(-1);
            orderPetAcquisitionMapper.update(orderPetAcquisition);
            // 抛出异常
            throw new BussinessException("你没在规定的时间内完成");
        }
        log.info("恭喜你成功完成订单了");
    }
}
